/*
 * @Author: lerio.cheng
 * @Date:   2017-02-16 09:31:28
 * @Last Modified by:   DELL
 * @Last Modified time: 2017-02-24 12:49:37
 */
'use strict';

app.controller('editHouseCtrl', function($scope, $http, rest, $location, ngDialog, user, $state, tipService, $window, uploadConf, $rootScope) {
    $scope.params = {
        landlordId: user.get('landlordId')
    };
    // $scope.params.housePic = [];
    if ($window.localStorage.getItem("houseId")) {
        $scope.houseIdInfo = JSON.parse($window.localStorage.getItem("houseId"));
    }

    $scope.column2 = 0;      //分栏
    $scope.maxImgNum = 5;    //最大图片数量
    $scope.imgNew= [
        {
            text:'卧室图片',
            name: 'bedRoom',
            value: [],
            picType: 0,
            hink: '至少两张'
        }, 
        {
            text:'客厅图片',
            name: 'livingRoom',
            value: [],
            picType: 1,
            hink: '至少两张'
        },
        {
            text:'卫生间图片',
            name: 'bathRoom',
            value: [],
            picType: 2,
            hink: '至少两张'
        },
        {
            text:'厨房图片',
            name: 'kitchenRoom',
            value: [],
            picType: 3,
            hink: '至少两张'
        },
        {
            text:'周边环境',
            name: 'environment',
            value: [],
            picType: 4,
            hink: '至少两张'
        },
        {
            text:'房门照片',
            name: 'door',
            value: [],
            picType: 10,
            hink: '至少两张',
            pdf:1
        }
    ];
    uploadConf.column2 = $scope.column2;
    uploadConf.maxImgNum =  $scope.maxImgNum;
    uploadConf.imgNew = $scope.imgNew; 



    rest.post({
        methodId: '4014',
        data: {
            houseId: $scope.houseIdInfo.id
        },
        success: function(res) {
            if (res.code == 0) {
                // $scope.params = res.res.data;
                console.log(res);
                var houseDeviceData = res.res.data.houseSource;
                $scope.houseId = $scope.params.houseId;
                $scope.bedRoom = $scope.bedRoom || [];
                $scope.livingRoom = $scope.livingRoom || [];
                $scope.bathRoom = $scope.bathRoom || [];
                $scope.kitchenRoom = $scope.kitchenRoom || [];
                $scope.environment = $scope.environment || [];

                $scope.params = res.res.data.houseSource;
                $scope.provinceId = $scope.params.provinceId;
                $scope.cityId = $scope.params.cityId;
                $scope.countyId = $scope.params.countyId;

                for (var i in $scope.params.housePic) {
                    if ($scope.params.housePic[i].picType == 0) {
                        $scope.imgNew[0].value.push($scope.params.housePic[i].picPath);
                    } else if ($scope.params.housePic[i].picType == 1) {
                        $scope.imgNew[1].value.push($scope.params.housePic[i].picPath);
                    } else if ($scope.params.housePic[i].picType == 2) {
                        $scope.imgNew[2].value.push($scope.params.housePic[i].picPath);
                    } else if ($scope.params.housePic[i].picType == 3) {
                        $scope.imgNew[3].value.push($scope.params.housePic[i].picPath);
                    } else if ($scope.params.housePic[i].picType == 4) {
                        $scope.imgNew[4].value.push($scope.params.housePic[i].picPath);
                    } else if ($scope.params.housePic[i].picType == 10) {
                        $scope.imgNew[5].value.push($scope.params.housePic[i].picPath);
                    }
                }

                // 户型
                $scope.houseTypeValue = $scope.params.houseType;
                if ($scope.houseTypeValue + 1) {
                    $scope.houseTypeList.map(function(ele, index) {
                        if ($scope.houseTypeValue == ele.value) {
                            $scope.houseType.selected = ele;
                        }
                    })
                }
                //朝向
                $scope.orientationValue = $scope.params.orientation;
                if ($scope.orientationValue + 1) {
                    $scope.orientationList.map(function(ele, index) {
                        if ($scope.orientationValue == ele.value) {
                            $scope.orientation.selected = ele;
                        }
                    })
                }
                //装修
                $scope.renovationValue = $scope.params.renovation;
                if ($scope.renovationValue + 1) {
                    $scope.renovationList.map(function(ele, index) {
                        if ($scope.renovationValue == ele.value) {
                            $scope.renovation.selected = ele;
                        }
                    })
                }
                //applyType
                $scope.payTypeValue = $scope.params.payType;
                if ($scope.payTypeValue + 1) {
                    $scope.payTypeList.map(function(ele, index) {
                        if ($scope.payTypeValue == ele.value) {
                            $scope.payType.selected = ele;
                        }
                    })
                }
                // 押金
                $scope.depositValue = $scope.params.deposit;
                if ($scope.depositValue + 1) {
                    $scope.depositList.map(function(ele, index) {
                        if ($scope.depositValue == ele.value) {
                            $scope.deposit.selected = ele;
                        }
                    })
                }
                // 房屋设施
                // $scope.houseDevice.selected = [];
                // $scope.houseDeviceList.map(function(ele, index) {
                //     for (var i in $scope.params) {
                //         if (i == ele.selected && $scope.params[i] == 1) {
                //             $scope.houseDevice.selected.push(ele);
                //         }
                //     }
                // });
                
                $scope.bed = houseDeviceData.bed ? true : false;
                $scope.sofa = houseDeviceData.sofa ? true : false;
                $scope.board = houseDeviceData.board ? true : false;
                $scope.desk = houseDeviceData.desk ? true : false;
                $scope.chair = houseDeviceData.chair ? true : false;
                $scope.washMachine = houseDeviceData.washMachine ? true : false;
                $scope.refrigerator = houseDeviceData.refrigerator ? true : false;
                $scope.television = houseDeviceData.television ? true : false;
                $scope.airCondition = houseDeviceData.airCondition ? true : false;
                $scope.waterHeater = houseDeviceData.waterHeater ? true : false;
                $scope.lampblackMachine = houseDeviceData.lampblackMachine ? true : false;
                $scope.ambry = houseDeviceData.ambry ? true : false;
                $scope.wifi = houseDeviceData.wifi ? true : false;
                $scope.stove = houseDeviceData.stove ? true : false;
                $scope.microwaveOven = houseDeviceData.microwaveOven ? true : false;
                    
              // });

                // 出租类型
                $scope.leaseTypeValue = $scope.params.leaseType;
                if ($scope.leaseTypeValue + 1) {
                    $scope.leaseTypeList.map(function(ele, index) {
                        if ($scope.leaseTypeValue == ele.value) {
                            $scope.leaseType.selected = ele;
                        }
                    })
                }
                // 是否已出租
                $scope.isRentValue = $scope.params.isRent;
                if ($scope.isRentValue + 1) {
                    $scope.isRentList.map(function(ele, index) {
                        if ($scope.isRentValue == ele.value) {
                            $scope.isRent.selected = ele;
                        }
                    })
                }
                // 是否可以随时入住
                $scope.isAnyTimeValue = $scope.params.isAnyTime;
                if ($scope.isAnyTimeValue + 1) {
                    $scope.isAnyTimeList.map(function(ele, index) {
                        if ($scope.isAnyTimeValue == ele.value) {
                            $scope.isAnyTime.selected = ele;
                        }
                    })
                }
            }
            $scope.$digest();            
            uploadConf.imgNew = $scope.imgNew;
            // console.log(uploadConf.imgNew);
        },
        error: function() {}
    })

    //获取房源模板列表
    var param_3012 = {};
    param_3012.landlordId = $scope.params.landlordId;
    rest.post({
        methodId: '3012',
        data: param_3012,
        success: function(res) {
            console.log( "3012",res );
            if (res.code == 0) {
                $scope.picModel = { selected: {name: '请选择模板', value: true} };
                $scope.picModelList = res.res.data;
                //console.log($scope.picModelList);
            } else {

            }
            $scope.disable_save_btn = false;
        },
        error: function() {
            $scope.disable_save_btn = false;
        }
    });

    $scope.chooseModel = function(index,picModelList) {
        $scope.editPicFlag = true;

        $scope.imgNew[0].value = [];
        $scope.imgNew[1].value = [];
        $scope.imgNew[2].value = [];
        $scope.imgNew[3].value = [];
        $scope.imgNew[4].value = [];
        $scope.imgNew[5].value = [];
        var picModelId = picModelList[index].id;
        // console.log( picModelId );

        var param_3013 = {};
        param_3013.id = picModelId;
        $rootScope.modelId = picModelId;

        rest.post({
            methodId: '3013',
            data: param_3013,
            success: function(res) {
                if (res.code == 0) {
                    var data = res.res.data.content;
                    data = JSON.parse( data );
                    for (var i in data) {
                        if (data[i].PicType == 0) {
                            $scope.imgNew[0].value.push(data[i].PicPath);
                        } else if (data[i].PicType == 1) {
                            $scope.imgNew[1].value.push(data[i].PicPath);
                        } else if (data[i].PicType == 2) {
                            $scope.imgNew[2].value.push(data[i].PicPath);
                        } else if (data[i].PicType == 3) {
                            $scope.imgNew[3].value.push(data[i].PicPath);
                        } else if (data[i].PicType == 4) {
                            $scope.imgNew[4].value.push(data[i].PicPath);
                        } else if (data[i].PicType == 10) {
                            $scope.imgNew[5].value.push(data[i].PicPath);
                        }
                    }
                }
                $scope.disable_save_btn = false;
                $scope.$apply();
            },
            error: function() {
                $scope.disable_save_btn = false;
            }
        });
        $scope.$apply();
    }

    // 户型
    $scope.houseType = {};
    $scope.houseTypeList = [{
        name: '一室一厅',
        value: 0
    }, {
        name: '一室半',
        value: 1
    }, {
        name: '二室一厅',
        value: 2
    }, {
        name: '二室半',
        value: 3
    }, {
        name: '三室一厅',
        value: 4
    }, {
        name: '四室以上',
        value: 5
    }, {
        name: '复式',
        value: 6
    }, {
        name: '一室户',
        value: 7
    }];
    // 朝向4013
    $scope.orientation = {};
    $scope.orientationList = [
    {
        name: '东',
        value: 0
    }, {
        name: '南',
        value: 1
    }, {
        name: '西',
        value: 2
    }, {
        name: '北',
        value: 3
    }, {
        name: '东南',
        value: 4
    }, {
        name: '西南',
        value: 5
    }, {
        name: '东北',
        value: 6
    }, {
        name: '西北',
        value: 7
    }, {
        name: '南北',
        value: 8
    }
    ];
    // 装修情况
    $scope.renovation = {};
    $scope.renovationList = [{
        name: '毛坯',
        value: 0
    }, {
        name: '简装',
        value: 1
    }, {
        name: '中等装修',
        value: 2
    }, {
        name: '精装修',
        value: 3
    }, {
        name: '豪华装修',
        value: 4
    }];
    // 支付方式
    $scope.payType = {};
    $scope.payTypeList = [{
        name: '一月一付',
        value: 0
    }, {
        name: '二月一付',
        value: 1
    }, {
        name: '三月一付',
        value: 2
    }];

    // 押金
    $scope.deposit = {};
    $scope.depositList = [{
        name: '无押金',
        value: 0
    }, {
        name: '一月押金',
        value: 1
    }, {
        name: '两月押金',
        value: 2
    }, {
        name: '三月押金',
        value: 3
    }];
    // 出租类型
    $scope.leaseType = {};
    $scope.leaseTypeList = [{
        name: '整租',
        value: 0
    }, {
        name: '合租',
        value: 1
    }];
    // 是否可以随时入住
    $scope.isAnyTime = {};
    $scope.isAnyTimeList = [{
        name: '是',
        value: 1
    }, {
        name: '否',
        value: 0
    }];
    // 是否已出租
    $scope.isRent = {};
    $scope.isRentList = [{
            name: '是',
            value: 1
        }, {
            name: '否',
            value: 0
        }]
        // 房屋设施
    $scope.houseDevice = {};
    $scope.houseDeviceList = [{
        name: '床',
        value: 'bed'
    }, {
        name: '沙发',
        value: 'sofa'
    }, {
        name: '餐桌',
        value: 'board'
    }, {
        name: '书桌',
        value: 'desk'
    }, {
        name: '椅子',
        value: 'chair'
    }, {
        name: '洗衣机',
        value: 'washMachine'
    }, {
        name: '冰箱',
        value: 'refrigerator'
    }, {
        name: '电视',
        value: 'television'
    }, {
        name: '空调',
        value: 'airCondition'
    }, {
        name: '热水器',
        value: 'waterHeater'
    }, {
        name: '油烟机',
        value: 'lampblackMachine'
    }, {
        name: '橱柜',
        value: 'ambry'
    }, {
        name: '无线网',
        value: 'wifi'
    }, {
        name: '燃气灶',
        value: 'stove'
    }, {
        name: '微波炉',
        value: 'microwaveOven'
    }];

    $scope.$watch('bedRoom',function(newVal,oldVal){
      $scope.bedRoom = newVal;
    });
    $scope.$watch('livingRoom',function(newVal,oldVal){
      $scope.livingRoom = newVal;
    });
    $scope.$watch('bathRoom',function(newVal,oldVal){
      $scope.bathRoom = newVal;
    });
    $scope.$watch('kitchenRoom',function(newVal,oldVal){
      $scope.kitchenRoom = newVal;
    });
    $scope.$watch('environment',function(newVal,oldVal){
      $scope.environment = newVal;
    });
    $scope.$watch('params.houseSize', function(newValue, oldValue) {               
         if (newValue < 1){
                $scope.params.houseSize =1;
        }
    });
    $scope.$watch('params.houseRent', function(newValue, oldValue) {               
         if (newValue < 1){
                $scope.params.houseRent =1;
        }
    });
    $scope.$watch('params.currFloor', function(newValue, oldValue) {               
         if (newValue < 1){
                $scope.params.currFloor =1;
        }
    });
    $scope.chooseAll = function(n,o){
        if ($scope.params.bed == 1) {
            $scope.params.bed = 0;
            $scope.bed = false;
        }else if($scope.params.bed == 0) {
            $scope.params.bed = 1;
            $scope.bed = true;
        }
        if ($scope.params.sofa == 1) {
            $scope.params.sofa = 0;
            $scope.sofa = false;
        }else if($scope.params.sofa == 0) {
            $scope.params.sofa = 1;
            $scope.sofa = true;
        }
        if ($scope.params.board == 1) {
            $scope.params.board = 0;
            $scope.board = false;
        }else if($scope.params.board == 0) {
            $scope.params.board = 1;
            $scope.board = true;
        }
        if ($scope.params.desk == 1) {
            $scope.params.desk = 0;
            $scope.desk = false;
        }else if($scope.params.desk == 0) {
            $scope.params.desk = 1;
            $scope.desk = true;
        }
        if ($scope.params.chair == 1) {
            $scope.params.chair = 0;
            $scope.chair = false;
        }else if($scope.params.chair == 0) {
            $scope.params.chair = 1;
            $scope.chair = true;
        }
        if ($scope.params.washMachine == 1) {
            $scope.params.washMachine = 0;
            $scope.washMachine = false;
        }else if($scope.params.washMachine == 0) {
            $scope.params.washMachine = 1;
            $scope.washMachine = true;
        }
        if ($scope.params.refrigerator == 1) {
            $scope.params.refrigerator = 0;
            $scope.refrigerator = false;
        }else if($scope.params.refrigerator == 0) {
            $scope.params.refrigerator = 1;
            $scope.refrigerator = true;
        }
        if ($scope.params.television == 1) {
            $scope.params.television = 0;
            $scope.television = false;
        }else if($scope.params.television == 0) {
            $scope.params.television = 1;
            $scope.television = true;
        }
        if ($scope.params.airCondition == 1) {
            $scope.params.airCondition = 0;
            $scope.airCondition = false;
        }else if($scope.params.airCondition == 0) {
            $scope.params.airCondition = 1;
            $scope.airCondition = true;
        }
        if ($scope.params.waterHeater == 1) {
            $scope.params.waterHeater = 0;
            $scope.waterHeater = false;
        }else if($scope.params.waterHeater == 0) {
            $scope.params.waterHeater = 1;
            $scope.waterHeater = true;
        }
        if ($scope.params.lampblackMachine == 1) {
            $scope.params.lampblackMachine = 0;
            $scope.lampblackMachine = false;
        }else if($scope.params.lampblackMachine == 0) {
            $scope.params.lampblackMachine = 1;
            $scope.lampblackMachine = true;
        }
        if ($scope.params.ambry == 1) {
            $scope.params.ambry = 0;
            $scope.ambry = false;
        }else if($scope.params.ambry == 0) {
            $scope.params.ambry = 1;
            $scope.ambry = true;
        }
        if ($scope.params.wifi == 1) {
            $scope.params.wifi = 0;
            $scope.wifi = false;
        }else if($scope.params.wifi == 0) {
            $scope.params.wifi = 1;
            $scope.wifi = true;
        }
        if ($scope.params.stove == 1) {
            $scope.params.stove = 0;
            $scope.stove = false;
        }else if($scope.params.stove == 0) {
            $scope.params.stove = 1;
            $scope.stove = true;
        }
        if ($scope.params.microwaveOven == 1) {
            $scope.params.microwaveOven = 0;
            $scope.microwaveOven = false;
        }else if($scope.params.microwaveOven == 0) {
            $scope.params.microwaveOven = 1;
            $scope.microwaveOven = true;
        }
    }

    $scope.$watch('bed',function(n,o){
        $scope.params.bed = n ? 1 : 0;
    });
    $scope.$watch('sofa',function(n,o){ 
        $scope.params.sofa = n ? 1 : 0;
    });
    $scope.$watch('board',function(n,o){ 
        $scope.params.board = n ? 1 : 0;
    });
    $scope.$watch('desk',function(n,o){ 
        $scope.params.desk = n ? 1 : 0;
    });
    $scope.$watch('chair',function(n,o){ 
        $scope.params.chair = n ? 1 : 0;
    });
    $scope.$watch('washMachine',function(n,o){ 
        $scope.params.washMachine = n ? 1 : 0;
    });
    $scope.$watch('refrigerator',function(n,o){ 
        $scope.params.refrigerator = n ? 1 : 0;
    });
    $scope.$watch('television',function(n,o){ 
        $scope.params.television = n ? 1 : 0;
    });
    $scope.$watch('airCondition',function(n,o){ 
        $scope.params.airCondition = n ? 1 : 0;
    });
    $scope.$watch('waterHeater',function(n,o){ 
        $scope.params.waterHeater = n ? 1 : 0;
    });
    $scope.$watch('lampblackMachine',function(n,o){ 
        $scope.params.lampblackMachine = n ? 1 : 0;
    });
    $scope.$watch('ambry',function(n,o){ 
        $scope.params.ambry = n ? 1 : 0;
    });
    $scope.$watch('wifi',function(n,o){ 
        $scope.params.wifi = n ? 1 : 0;
    });
    $scope.$watch('stove',function(n,o){ 
        $scope.params.stove = n ? 1 : 0;
    });
    $scope.$watch('microwaveOven',function(n,o){ 
        $scope.params.microwaveOven = n ? 1 : 0;
    });
    $scope.saveData = function(a) {
        if(!$scope.bedRoom){
              tipService.show("body", {
                    message: "卧室数量必填",
                    notice:"danger"
                }, 3);
              return;
        }

        if(!$scope._provinceSelected.selected||!$scope._citySelected.selected||!$scope._areaSelected.selected){
              tipService.show("body", {
                    message: "所在城市地址必须填全",
                    notice:"danger"
                }, 3);
              return;
          }
          // if(!$scope.params.houseName){
          //     tipService.show("body", {
          //           message: "房间名称不能为空",
          //           notice:"danger"
          //       }, 3);
          //     return;
          // }
          if(!$scope.houseType.selected || !$scope.orientation.selected || !$scope.params.houseSize){
              tipService.show("body", {
                    message: "户型、朝向、面积均不能不空",
                    notice:"danger"
                }, 3);
              return;
          }
          if(!$scope.renovation.selected){
              tipService.show("body", {
                    message: "装修情况不能为空",
                    notice:"danger"
                }, 3);
              return;
          }
          if (!$scope.params.currFloor || !$scope.params.floors || $scope.params.currFloor<=0 || $scope.params.floors<=0 || $scope.params.floors<$scope.params.currFloor) {
                tipService.show("body", {
                    message: "层高、总楼层数格式不正确",
                    notice: "danger"
                }, 3);
                return;
          }
          // var regMobile = /^1[3|4|5|7|8]\d{9}$/;
          // if (!$scope.params.landlordMobile || regMobile.test($scope.params.LandlordMobile)) {

          //   tipService.show("body", {
          //           message: "房东手机号码格式不正确",
          //           notice:"danger"
          //       }, 3);
          //     return;
          // }
          if(!$scope.leaseType.selected|| !$scope.params.houseRent || !$scope.isAnyTime.selected || !$scope.isRent.selected || !$scope.payType.selected || !$scope.deposit.selected || !$scope.params.roomCode){
            tipService.show("body", {
                    message: "出租信息栏信息未填全",
                    notice:"danger"
                }, 3);
              return;
          };

          for (var i in $scope.imgNew) {
              if ($scope.imgNew[i].value.length < 2) {
                tipService.show("body", {
                    message: "每项至少上传两张照片",
                    notice: "danger"
                }, 3);
                return;
              }
          }

        // if($scope.houseDevice.selected){
        //     for(var i in $scope.houseDevice.selected){
        //         $scope.params[$scope.houseDeviceList.checked] = 1;
        //     }
        // }

        $scope.params.provinceId = $scope._provinceSelected.selected.id;
        $scope.params.cityId = $scope._citySelected.selected.id;
        $scope.params.countyId = $scope._areaSelected.selected.id;
        $scope.params.orientation = $scope.orientation.selected.value;
        $scope.params.renovation = $scope.renovation.selected.value;
        $scope.params.payType = $scope.payType.selected.value;
        $scope.params.deposit = $scope.deposit.selected.value;
        $scope.params.houseType = $scope.houseType.selected.value;
        $scope.params.leaseType = $scope.leaseType.selected.value;
        $scope.params.isAnyTime = $scope.isAnyTime.selected.value;
        $scope.params.isRent = $scope.isRent.selected.value;
        //照片上传
        $scope.params.housePic.splice(0,$scope.params.housePic.length);
        for (var i in uploadConf.imgNew) {
            for (var j in uploadConf.imgNew[i].value) {
                $scope.params.housePic.push({picPath: uploadConf.imgNew[i].value[j],picType: uploadConf.imgNew[i].picType, sort: 0});
            }
        }
        $scope.params = angular.extend({ LandlordId: user.get('landlordId') }, $scope.params);
        console.log($scope.params);
        rest.post({
            methodId: '4013',
            data: $scope.params,
            success: function(res) {
            	if (res.code == 0) {
	                tipService.show("body", {
	                    message: "修改数据成功,跳转中......"
	                }, 3);
	                $location.path('/app/houseOwner/list');
                } else {
                    tipService.show("body", {
                        message: "房源数据修改失败!",
                        notice: "danger"
                    }, 3);
                }
            },
            error: function() {}
       });
    };

    $scope.backList = function(e){
        $location.path('/app/houseOwner/list');
    }

    $scope.addModelPics = function () {
        $state.go('app.houseOwner.housePicsTemplet');
    }

    $scope.editModelPics = function(templetId){
        $window.localStorage.setItem('templetId',JSON.stringify({id:$rootScope.modelId}));
        $state.go('app.houseOwner.editHousePicsTemplet',{id: $rootScope.modelId});
        // console.log($rootScope.modelId);
    }

})
